package com.final_project.zombiemenace;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Typeface;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;

import com.final_project.zombiemenace.highscore.HighScoreHandler;
import com.final_project.zombiemenace.highscore.HighScoreThread;
import com.final_project.zombiemenace.highscore.IHighscoreHandler;
import com.final_project.zombiemenace.settings.Settings;

public class MainMenuActivity extends Activity implements View.OnClickListener {
	static int currentMusicPossition;
	static boolean continueBGMusic;
	public static boolean vibration;
	public static boolean isMuted;
	public static float musicVolume;
	private float systemVolume;
	ScrollView scroll;
	TextView titleHighscore;
	TextView highScoreText;
	Thread highscoresThread;
	AudioManager audioManager;
	Settings settings;
	IHighscoreHandler handler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		settings = new Settings(this);
		audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
		systemVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
		MainMenuActivity.continueBGMusic = true;
		setContentView(R.layout.activity_main_menu);
		setButtonsOnClickListeners();
		getSettings();
		// initialize high score handler
		handler = new HighScoreHandler(this);
		// initialize views
		scroll = (ScrollView) findViewById(R.id.scrollView);
		titleHighscore = (TextView) findViewById(R.id.title);
		highScoreText = (TextView) scroll.getChildAt(0);
		highScoreText.setText(handler.getHighScoresAsString());
		// set Bloody font
		setTextFont(titleHighscore);
		setTextFont(highScoreText);
	}

	private void getSettings() {
		vibration = settings.getIsGoingToVibrateFromSharedPrefs();
		isMuted = settings.getIsMutedFromSharedPrefs();
		musicVolume = settings.getVolumeFromSharedPrefs();
	}

	public void setTextFont(TextView tv) {
		tv.setTypeface(Typeface.createFromAsset(getAssets(), "bloody.ttf"));
	}

	@Override
	public void onPause() {
		super.onPause();
		HighScoreThread.stop();
		if (!MainMenuActivity.continueBGMusic)
			MusicManager.pause();
		AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
		audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
				(int) systemVolume, 0);
		settings.writeSettingsToFile();
	}

	@Override
	public void onResume() {
		super.onResume();
		highscoresThread = new Thread(
				new HighScoreThread(scroll, highScoreText));
		highscoresThread.start();
		MainMenuActivity.continueBGMusic = false;
		MusicManager.start(this, R.raw.dst_the_haunted_chapel);
		AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
		setVolume(audioManager);
	}

	private void setVolume(AudioManager audioManager) {
		audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
				(int) MainMenuActivity.musicVolume, 0);
	}

	private void setButtonsOnClickListeners() {
		Button aboutButton = (Button) findViewById(R.id.aboutButton);
		aboutButton.setTypeface(Typeface.createFromAsset(getAssets(), "bloody.ttf"));
		Button playButton = (Button) findViewById(R.id.playButton);
		playButton.setTypeface(Typeface.createFromAsset(getAssets(), "bloody.ttf"));
		Button optionsButton = (Button) findViewById(R.id.optionsButton);
		optionsButton.setTypeface(Typeface.createFromAsset(getAssets(), "bloody.ttf"));
		aboutButton.setOnClickListener(this);
		playButton.setOnClickListener(this);
		optionsButton.setOnClickListener(this);
	}

	protected void showAbout() {
		String versionName = checkForVersionName();
		View messageView = getLayoutInflater().inflate(R.layout.about, null,
				false);
		TextView aboutVersiontextView = (TextView) messageView
				.findViewById(R.id.about_version);
		aboutVersiontextView.setText(aboutVersiontextView.getText().toString()
				+ " " + versionName);
		createAboutDialog(messageView);
	}

	private String checkForVersionName() {
		String versionName = "";
		try {
			versionName = getPackageManager().getPackageInfo(getPackageName(),
					0).versionName;
			if (versionName == null) {
				return "Cannot determine version";
			}
			return versionName;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return versionName;
	}

	private void createAboutDialog(View messageView) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setIcon(R.drawable.ic_launcher);
		builder.setTitle(R.string.app_name);
		builder.setView(messageView);
		final AlertDialog alert = builder.create();
		builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				alert.dismiss();
			}
		});
		builder.show();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main_menu, menu);
		return true;
	}

	public void onClick(View v) {
		Intent playerChoice;
		switch (v.getId()) {
		case R.id.playButton:
			playerChoice = new Intent(MainMenuActivity.this, PlayTheGame.class);
			startActivity(playerChoice);
			break;
		case R.id.optionsButton:
			playerChoice = new Intent(MainMenuActivity.this, Options.class);
			startActivity(playerChoice);
			break;
		case R.id.aboutButton:
			showAbout();
			break;
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
			if (MainMenuActivity.musicVolume <=100) {
				MainMenuActivity.musicVolume += 1;
				audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
						(int) MainMenuActivity.musicVolume,
						0);
			}
			return true;
		} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
			if (MainMenuActivity.musicVolume >= 0) {
				MainMenuActivity.musicVolume -= 1;
				audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
						(int) MainMenuActivity.musicVolume,
						0);
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}